using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using System.IO;

namespace SPARParser
{
    public class Parser
    {
        public static MatrixDataStructures.SingleVolume Parse(string args)
        {
            string fileName = args.ToString();

            if (!System.IO.File.Exists(fileName))
                return null;

            string[] strArray = System.IO.File.ReadAllLines(fileName);
            int strNumber;
            bool strIndex = true;
            double ap_size = new double();
            double lr_size = new double();
            double cc_size = new double();
            double ap_off_center = new double();
            double lr_off_center = new double();
            double cc_off_center = new double();
            double ap_angulation = new double();
            double lr_angulation= new double();
            double cc_angulation  = new double();
            for (strNumber = 0; strNumber < strArray.Length; strNumber++)
            {
                try
                {
                    strIndex = (strArray[strNumber].Substring(0, 7) == "ap_size");
                    if (strIndex == true)
                    {
                        ap_size = double.Parse(strArray[strNumber].Substring(9).Trim());
                        File.WriteAllText(System.IO.Path.GetDirectoryName(fileName) +"\\log.txt","ap_size: " + ap_size.ToString() + "\n");
                        continue;
                    }
                    strIndex = (strArray[strNumber].Substring(0, 7) == "lr_size");
                    if (strIndex == true)
                    {
                        lr_size = double.Parse(strArray[strNumber].Substring(9).Trim());
                        File.AppendAllText(System.IO.Path.GetDirectoryName(fileName) + "\\log.txt", "lr_size: " + lr_size.ToString() +"\n");
                        continue;
                    }
                    strIndex = (strArray[strNumber].Substring(0, 7) == "cc_size");
                    if (strIndex == true)
                    {
                        cc_size = double.Parse(strArray[strNumber].Substring(9).Trim());
                        File.AppendAllText(System.IO.Path.GetDirectoryName(fileName) + "\\log.txt", "cc_size: " + cc_size.ToString()+"\n");
                        continue;
                    }
                    strIndex = (strArray[strNumber].Substring(0, 13) == "ap_off_center");
                    if (strIndex == true)
                    {
                        ap_off_center = double.Parse(strArray[strNumber].Substring(15).Trim());
                        File.AppendAllText(System.IO.Path.GetDirectoryName(fileName) + "\\log.txt", "ap_off_center: " + ap_off_center.ToString()+"\n");
                        continue;
                    }
                    strIndex = (strArray[strNumber].Substring(0, 13) == "lr_off_center");
                    if (strIndex == true)
                    {
                        lr_off_center = double.Parse(strArray[strNumber].Substring(15).Trim());
                        File.AppendAllText(System.IO.Path.GetDirectoryName(fileName) + "\\log.txt", "lr_off_center: " + lr_off_center.ToString()+"\n");
                        continue;
                    }
                    strIndex = (strArray[strNumber].Substring(0, 13) == "cc_off_center");
                    if (strIndex == true)
                    {
                        cc_off_center = double.Parse(strArray[strNumber].Substring(15).Trim());
                        File.AppendAllText(System.IO.Path.GetDirectoryName(fileName) + "\\log.txt", "cc_off_center: " + cc_off_center.ToString()+"\n");
                        continue;
                    }
                    strIndex = (strArray[strNumber].Substring(0, 13) == "ap_angulation");
                    if (strIndex == true)
                    {
                        ap_angulation = double.Parse(strArray[strNumber].Substring(15).Trim());
                        File.AppendAllText(System.IO.Path.GetDirectoryName(fileName) + "\\log.txt", "ap_angulation: " + ap_angulation.ToString()+"\n");
                        continue;
                    }
                    strIndex = (strArray[strNumber].Substring(0, 13) == "lr_angulation");
                    if (strIndex == true)
                    {
                        lr_angulation = double.Parse(strArray[strNumber].Substring(15).Trim());
                        File.AppendAllText(System.IO.Path.GetDirectoryName(fileName) + "\\log.txt", "lr_angulation: " + lr_angulation.ToString()+"\n");
                        continue;
                    }
                    strIndex = (strArray[strNumber].Substring(0, 13) == "cc_angulation");
                    if (strIndex == true)
                    {
                        cc_angulation = double.Parse(strArray[strNumber].Substring(15).Trim());
                        File.AppendAllText(System.IO.Path.GetDirectoryName(fileName) + "\\log.txt", "cc_angulation: " + cc_angulation.ToString()+"\n");
                        continue;
                    }
                }
                catch //(System.Exception e)
                {
                    //Console.WriteLine("Error: " + e.Message);
                    continue;
                }                
            }
            Console.WriteLine("Initializing matrix...");
            
            MatrixDataStructures.SingleVolume filter = new MatrixDataStructures.SingleVolume();

            filter.FOV = new MatrixDataStructures.CoordinateD((lr_size), (ap_size), (cc_size));
            lr_size = Math.Round(lr_size, 0);
            ap_size = Math.Round(ap_size, 0);
            cc_size = Math.Round(cc_size, 0);
            filter.Matrix = new MatrixDataStructures.Matrix3DInt16(Convert.ToInt16(lr_size), Convert.ToInt16(ap_size), Convert.ToInt16(cc_size));
            filter.Center = new MatrixDataStructures.CoordinateD((lr_off_center), (ap_off_center), (cc_off_center));
            filter.OffCenterAngle = new MatrixDataStructures.CoordinateD(lr_angulation, ap_angulation, cc_angulation);
            for (int i = 0; i < filter.Matrix.x; i++)
                for (int j = 0; j < filter.Matrix.y; j++)
                    for (int k = 0; k < filter.Matrix.z; k++)
                        filter.Matrix[i, j, k] = 1;
            Console.WriteLine("Initialization complete.");
            return filter;
        }
    }
}
